package com.example.userblog.controller;

import com.example.userblog.common.Result;
import com.example.userblog.common.config.JwtTokenUtils;
import com.example.userblog.entity.User;
import com.example.userblog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;
    @GetMapping("/list")
    public Result getData(){
        List<User> users=userService.getAll();

        return Result.success(userService.getAll());


    }
    @PostMapping("/register")
    public Result userRegister(@RequestBody User user){
        return Result.success(userService.userRegister(user));

    }
    @PostMapping("/login")
    public Result userLogin(@RequestBody User user){
        User user1=userService.userLogin(user);
        String token= JwtTokenUtils.genToken(user1.getId()+"-"+user.getRole(),user.getPassword());

        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("user",user1);
        return Result.success(map);
    }


}
